package com.game.business.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.hutool.core.text.split.SplitIter;
import com.game.common.annotation.Log;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import com.game.common.core.domain.entity.SysUser;
import com.game.common.enums.BusinessType;
import com.game.common.utils.poi.ExcelUtil;
import javax.servlet.http.HttpServletResponse;

import com.game.system.service.ISysUserService;
import com.game.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.game.business.domain.TNotice;
import com.game.business.service.ITNoticeService;
import com.game.common.core.page.TableDataInfo;

/**
 * 系统消息Controller
 *
 * @author ruoyi
 * @date 2025-10-22
 */
@RestController
@RequestMapping("/business/xtnotice")
public class TNoticeController extends BaseController {
    @Autowired
    private ITNoticeService tNoticeService;

/**
 * 查询系统消息列表
 */
//@RequiresPermissions("business:xtnotice:list")
@GetMapping("/list")
    public TableDataInfo list(TNotice tNotice) {
        startPage();
        List<TNotice> list = tNoticeService.selectTNoticeList(tNotice);
        return getDataTable(list);
    }

    /**
     * 导出系统消息列表
     */
    //@RequiresPermissions("business:xtnotice:export")
    @Log(title = "系统消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TNotice tNotice) {
        List<TNotice> list = tNoticeService.selectTNoticeList(tNotice);
        ExcelUtil<TNotice> util = new ExcelUtil<TNotice>(TNotice. class);
        util.exportExcel(response, list, "系统消息数据");
    }

    /**
     * 获取系统消息详细信息
     */
    //@RequiresPermissions("business:xtnotice:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(tNoticeService.selectTNoticeById(id));
    }


    @Autowired
    private ISysUserService sysUserService;
    /**
     * 新增系统消息
     */
    //@RequiresPermissions("business:xtnotice:add")
    @Log(title = "系统消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TNotice vo) {
        String tilte = vo.getTilte();
        String content = vo.getContent();
        String userType = vo.getUserType();
        String[] split = userType.split(",");
        List<String> userTypeList = Arrays.asList(split);
        Boolean  needdbr = false;
        Boolean  needdls = false;
        Boolean  needuser = false;
        if(userTypeList.contains("1")){
            needdbr=true;
        }
        if(userTypeList.contains("2")){
            needdls=true;
        }
        if(userTypeList.contains("4")){
            needuser=true;
        }
        SysUser sysUser = new SysUser();
        List<SysUser> sysUsers = sysUserService.selectUserListApp(sysUser);
        List<SysUser> sysUsersSend = new ArrayList<>();
        for (int i = 0; i < sysUsers.size(); i++) {
            SysUser sysUser1 = sysUsers.get(i);
            String isDbr = sysUser1.getIsDbr();
            String isDls = sysUser1.getIsDls();
            if(isDbr.equals("1")  &&  needdbr){
                sysUsersSend.add(sysUser1);
            }
            if(isDls.equals("1")  &&  needdls){
                sysUsersSend.add(sysUser1);
            }
            if(needuser  &&  !isDbr.equals("1")  &&  !isDls.equals("1")){
                sysUsersSend.add(sysUser1);
            }
        }
        sysUsersSend = sysUsersSend.stream()
                .distinct()
                .collect(Collectors.toList());
        for (SysUser user : sysUsersSend) {
            TNotice notice = new TNotice();
            notice.setTilte(tilte);
            notice.setContent(content);
            notice.setReceiveUserId(user.getUserId().toString());
            notice.setSendUserId(getUserId().toString()); // 当前登录用户ID
            notice.setType(vo.getType());
            notice.setBusinessId(vo.getBusinessId());
            notice.setIsRead("0"); // 未读状态
            tNoticeService.save(notice);
        }
        return AjaxResult.success();
    }

    /**
     * 修改系统消息
     */
    //@RequiresPermissions("business:xtnotice:edit")
    @Log(title = "系统消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TNotice tNotice) {
        return toAjax(tNoticeService.updateById(tNotice));
    }

    /**
     * 删除系统消息
     */
    //@RequiresPermissions("business:xtnotice:remove")
    @Log(title = "系统消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tNoticeService.removeByIds(Arrays.asList(ids)));
    }
}
